package cn.schoolwow.flow.monitor.module.initial.config;

import cn.schoolwow.flow.monitor.domain.handler.FlowMonitorFlowMappingHandler;
import cn.schoolwow.flow.monitor.domain.handler.FlowMonitorQuickFlowCommonTryCatchFinally;
import cn.schoolwow.flow.monitor.domain.handler.FlowMonitorQuickFlowTestCaseTryCatchFinally;
import cn.schoolwow.flow.monitor.domain.handler.TaskLogTryCatchFinallyHandler;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.initial.flow.InitialScheduleJobFlow;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

@DependsOn("flowMonitorOption")
@Service
public class InitialFlowMonitorOptionFlow implements InitializingBean {
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
        setFlow(flowMonitorOption);
        initialScheduleJob(flowMonitorOption);
    }

    private void setFlow(FlowMonitorOption flowMonitorOption){
        if(null==flowMonitorOption.basePackageName){
            throw new IllegalArgumentException("请指定FlowMonitorOption对象的basePackageName属性");
        }
        if(null==flowMonitorOption.logQuickFlow){
            flowMonitorOption.logQuickFlow = QuickFlowBuilder.newInstance()
                    .tryCatchFinallyHandler(new FlowMonitorQuickFlowCommonTryCatchFinally())
                    .flowNameInstanceMappingHandler(applicationContext.getBean(FlowMonitorFlowMappingHandler.class))
                    .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                    .putContextTemporaryInstanceData(flowMonitorOption);
        }
        QuickFlow quickFlow = QuickFlowBuilder.newInstance()
                .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                .putContextTemporaryInstanceData(flowMonitorOption);
        if(null==flowMonitorOption.flowRecordQuickFlow){
            flowMonitorOption.flowRecordQuickFlow = quickFlow;
        }
        if(null==flowMonitorOption.testcaseQuickFlow){
            flowMonitorOption.testcaseQuickFlow = QuickFlowBuilder.newInstance()
                    .tryCatchFinallyHandler(new FlowMonitorQuickFlowTestCaseTryCatchFinally())
                    .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                    .putContextTemporaryInstanceData(flowMonitorOption);
        }
        if(null==flowMonitorOption.scheduleQuickFlow){
            flowMonitorOption.scheduleQuickFlow = QuickFlowBuilder.newInstance()
                    .tryCatchFinallyHandler(new TaskLogTryCatchFinallyHandler())
                    .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                    .putContextTemporaryInstanceData(flowMonitorOption);
        }
        flowMonitorOption.flowMonitorQuickFlow = quickFlow;
    }

    private void initialScheduleJob(FlowMonitorOption flowMonitorOption){
        flowMonitorOption.flowMonitorQuickFlow.startFlow(new InitialScheduleJobFlow()).execute();
    }
}
